#define PC

using System;
using System.Collections.Generic;
#if PC
using System.Diagnostics;
#endif
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

namespace TimeWar
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class ChooseScenario : CurrGameComponent
    {

        private SpriteBatch spriteBatch;
        private TimeWar timeWarGame;
        private Vector2 timeLordOrgPos, dalekOrgPos;
        private Texture2D timeLordSide, dalekSide, tardisCursor, dalekCursor, drCursor;
        private int screenheight, screenwidth;

        SpriteFont timeLordFont, dalekFont;
        Vector2 timeLordFontPos, dalekFontPos;

        


        public ChooseScenario(TimeWar game)
            : base(game)
        {
            // TODO: Construct any child components here
            timeWarGame = game;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.gameComponentInitialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
#if PC
            Trace.WriteLine("[" + DateTime.Now + "]: Calling ChooseScenario.Update().");
            Trace.WriteLine("[" + DateTime.Now + "]: GameTime: " + gameTime.ElapsedGameTime.ToString());

            if (Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                Trace.WriteLine("[" + DateTime.Now + "]: Mouse left button pressed detected in ChooseScenario.Update().");
                //choiceGoodBadScreen.Enabled = false;
                //choiceGoodBadScreen.Draw(graphics);
                //currGameComponent = choiceGoodBadScreen;

                Vector2 cursorPosition = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);

                if (cursorPosition.X < timeLordSide.Width)
                {
                    Trace.WriteLine("[" + DateTime.Now + "]: Selected the side of the Time Lords.");
                    timeWarGame.setGameMapToCurrGameComponent(0);

                }
                else if (cursorPosition.X > screenwidth - dalekSide.Width)
                {
                    Trace.WriteLine("[" + DateTime.Now + "]: Selected the side of the Daleks.");
                    timeWarGame.setGameMapToCurrGameComponent(1);

                }
                else
                {
                    Trace.WriteLine("[" + DateTime.Now + "]: Mouse left button pushed over empty area.");

                }



            }




            Trace.WriteLine("[" + DateTime.Now + "]: Leaving ChooseScenario.Update().");

#endif

            base.gameComponentUpdate(gameTime);
        }

        public override void Draw(GraphicsDeviceManager graphics, GameTime gameTime)
        {
#if PC
            Trace.WriteLine("[" + DateTime.Now + "]: Calling ChooseScenario.Draw().");
#endif
            //graphics.GraphicsDevice.Reset();
            this.Enabled = false;
            graphics.GraphicsDevice.Clear(Color.Black);
#if PC
            Trace.WriteLine("[" + DateTime.Now + "]: Called graphics.GraphicsDevice.Clear(Color.Black).");
#endif

            //graphics.GraphicsDevice.Present();
#if PC
            Trace.WriteLine("[" + DateTime.Now + "]: Called graphics.GraphicsDevice.Present().");
#endif


            // TODO: Add your drawing code here
            if (spriteBatch != null)
            {
                spriteBatch.Begin();

                //Drawing code fits in here.
                Vector2 timeLordCenter = new Vector2(timeLordSide.Height / 2, timeLordSide.Width / 2);
                //spriteBatch.Draw(timeLordSide, timeLordOrgPos, null, Color.White, 0, timeLordCenter, 1, SpriteEffects.None, 0f);

                // Draw Hello World
                string timeLordText = "Time Lords";

                // Find the center of the string
                Vector2 timeLordFontOrigin = timeLordFont.MeasureString(timeLordText) / 2;

                //Draw Dalek side
                Vector2 dalekCenter = new Vector2(screenwidth - (dalekSide.Width / 2), dalekSide.Height / 2);

                string dalekText = "Daleks";

                Vector2 dalekFontOrigin = dalekFont.MeasureString(dalekText) / 2;


                //Draw tardis cursor at mouse position...game position
#if PC
                Vector2 cursorPosition = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
#endif
                //check position of cursor before determining texture to draw
                if (cursorPosition.X < timeLordSide.Width)
                {
                    spriteBatch.Draw(timeLordSide, timeLordOrgPos, Color.LightGreen);

                    spriteBatch.Draw(dalekSide, dalekOrgPos, Color.White);
                    
                    spriteBatch.Draw(drCursor, cursorPosition, Color.White);
                }
                else if (cursorPosition.X > screenwidth - dalekSide.Width)
                {
                    spriteBatch.Draw(timeLordSide, timeLordOrgPos, Color.White);

                    spriteBatch.Draw(dalekSide, dalekOrgPos, Color.LightGreen);


                    spriteBatch.Draw(dalekCursor, cursorPosition, Color.White);
                }
                else
                {
                    spriteBatch.Draw(timeLordSide, timeLordOrgPos, Color.White);

                    spriteBatch.Draw(dalekSide, dalekOrgPos, Color.White);
                   
                    spriteBatch.Draw(tardisCursor, cursorPosition, Color.White);
                }

                spriteBatch.DrawString(timeLordFont, timeLordText, timeLordFontPos, Color.Tomato, 0, timeLordFontOrigin, 1.0f, SpriteEffects.None, 1.0f);
                spriteBatch.DrawString(dalekFont, dalekText, dalekFontPos, Color.Tomato, 0, dalekFontOrigin, 1.0f, SpriteEffects.None, 1.0f);


                
                
                
                
                
                
                
                spriteBatch.End();
            }


#if PC
            Trace.WriteLine("[" + DateTime.Now + "]: Leaving ChooseScenario.Draw().");
#endif


        }

        public override void LoadContent(GraphicsDevice graphicsDevice)
        {
            spriteBatch = new SpriteBatch(graphicsDevice);
            timeLordFont = timeWarGame.Content.Load<SpriteFont>("Courier New");
            dalekFont = timeWarGame.Content.Load<SpriteFont>("Courier New");


            // TODO: use this.Content to load your game content here
            //Content.
            timeLordSide = timeWarGame.Content.Load<Texture2D>("Images\\ChooseTimeLords");
            screenheight = graphicsDevice.Viewport.Height;
            screenwidth = graphicsDevice.Viewport.Width;
            // Set the origin so that we're drawing from the 
            // center of the top edge.
            timeLordOrgPos = new Vector2(0, 0);
            // Set the screen position to the center of the screen.
            timeLordFontPos = new Vector2(timeLordSide.Width / 2, timeLordSide.Height / 2);
            //screenpos = new Vector2(0, 0);
            //screenpos = new Vector2(screenwidth / 2 - (background.Width / 2), screenheight / 2 - (background.Height / 2));

            dalekSide = timeWarGame.Content.Load<Texture2D>("Images\\ChooseDaleks");
            dalekOrgPos = new Vector2(screenwidth - dalekSide.Width, 0);
            dalekFontPos = new Vector2(screenwidth - (dalekSide.Width / 2), dalekSide.Height / 2);

            tardisCursor = timeWarGame.Content.Load<Texture2D>("Cursors\\tardis");
            drCursor = timeWarGame.Content.Load<Texture2D>("Cursors\\9thDr");
            dalekCursor = timeWarGame.Content.Load<Texture2D>("Cursors\\dalekSec");

            


        }

    }
}